package com.xing.code.main;

import com.xing.code.bean.Code;
import com.xing.code.dao.CodeDao;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2015/3/13 0013.
 */
public class Main {
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
    private static CodeDao codeDao = (CodeDao) ac.getBean("codeDao");
    private static SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

    public static void main(String[] args) throws Exception {
        String base = "http://www.libao.so/index.php?page=";
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet();
        CloseableHttpResponse response = null;
        String result = null;
        for (int i = 1; i < 10; i++) {
            String page = base + i;
            URI uri = new URI(page);
            httpGet.setURI(uri);
            response = httpClient.execute(httpGet);
            HttpEntity re = response.getEntity();
            result = EntityUtils.toString(re, "utf-8");
            Document document = Jsoup.parse(result);
            Elements elements = document.select("table.lbtable > tbody > tr");
            for (Element element : elements) {
                if (element.getElementsByTag("td") != null && element.getElementsByTag("td").size() > 0 && !BerkeleyDBUtil.getInstance().has(element.getElementsByTag("td").get(2).text())) {
                    Code code = new Code();
                    code.setTitle(element.getElementsByTag("td").get(0).text());
                    code.setSize(element.getElementsByTag("td").get(1).text());
                    code.setCode(element.getElementsByTag("td").get(2).text());
                    Date date = bartDateFormat.parse(element.getElementsByTag("td").get(3).text());
                    int time = (int)(date.getTime()/1000);
                    code.setTime(time);
                    codeDao.saveCode(code);
//                    System.out.println(element.getElementsByTag("td").get(0).text() + " - " + element.getElementsByTag("td").get(1).text()
//                            + " - " + element.getElementsByTag("td").get(2).text() + " - " + element.getElementsByTag("td").get(3).text() + " page: " + i);
                }
            }
            System.out.println("the page: " + i);
        }
    }
}
